/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */

package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.MenuItem;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ouertech.android.agm.lib.base.utils.UtilNetwork;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * @author : Zhenshui.Xia
 * @date   :  2014年12月24日
 * @desc   : 公共的web界面
 */
public class WebActivity extends BaseTopActivity{
    @Bind(R.id.web_id_webview)
    WebView mWebView;

    private String mUrl;
    private String mTitle;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);

        Intent intent = getIntent();
        mUrl = intent.getStringExtra(CstOuer.KEY.URL);
        mTitle = intent.getStringExtra(CstOuer.KEY.TITLE);
    }

    @Override
    protected void initTop() {
        setTitle(mTitle);
        //返回
        setNavigation(R.drawable.common_ic_left_nav, new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                }else{
                    finish();
                }

                return false;
            }
        });

        //重试
        setOnRetryListener(new OnRetryListener() {

            @Override
            public void onRetry() {
                loadUrl(mUrl);
            }
        });
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_web);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new CommonWebViewClient());
        mWebView.setWebChromeClient(new CommonWebChromeClient());
        loadUrl(mUrl);
    }


    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return;
        }

        super.onBackPressed();
    }

    /**
     * @author Zhenshui.xia
     */
    private class CommonWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int progress) {
            super.onProgressChanged(view, progress);

            if(progress == 100) {
                setLoading(false);
            }
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if(UtilString.isBlank(mTitle)) {
                setTitle(title);
            }
        }

    }

    /**
     * @author Zhenshui.xia
     */
    private class CommonWebViewClient extends WebViewClient {

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

        }

        @Override
        public void onPageFinished(WebView view, String url) {

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return OuerDispatcher.localUriLoading(WebActivity.this, url);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                       SslError error) {
            handler.proceed();
        }

    }

    private void loadUrl(String url) {
        if(UtilNetwork.isNetAvailable(this)) {
            setLoading(true);
            mWebView.loadUrl(url);
        } else {
            setRetry(true);
            UtilOuer.toast(this, R.string.res_string_network_unavaiable);
        }
    }
}
